package com.xls.pet.listener;

import com.mybatisflex.annotation.SetListener;
import com.xls.pet.entity.ItemStock;

import java.sql.Date;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class ItemStockOnSetListener implements SetListener {
    @Override
    public Object onSet(Object entity, String property, Object value) {
        ItemStock itemStock = (ItemStock) entity;
        if (property.equals("expirationDate") && value != null) {
            Date expirationDate = (Date) value;
            Integer between = (int) ChronoUnit.DAYS.between(LocalDate.now(), expirationDate.toLocalDate());
            itemStock.setRemainingDays(between);
        }
        return value;
    }
}